package com.iov.failurelog.rabbitmq;

import com.alibaba.fastjson.JSON;
import com.iov.failurelog.controller.HbaseController;
import com.iov.failurelog.domain.TbFailurelog;
import com.iov.failurelog.mapper.TbFailurelogMapper;
import com.iov.failurelog.utils.PoolDemoThread;
import com.iov.failurelog.utils.RedisUtil;
import com.iov.failurelog.vo.VehicleMsgEntity;
import com.rabbitmq.client.Channel;
import org.apache.tomcat.util.threads.ThreadPoolExecutor;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.core.GeoOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

import static cn.hutool.core.thread.GlobalThreadPool.submit;


/**
 * @Author:Adminster
 * @Date:2023/11/19
 * @Description:
 * @Verson:3.6.3
 */
@Component
public class ConsumptionMq {
    @Resource
    private RedisTemplate redisTemplate = RedisUtil.redis;

    @Resource
    private TbFailurelogMapper faultLogMapper;

    @Resource
    private PoolDemoThread poolDemoThread;


    /* 故障信息  redis删除 数据库修改*/
    @RabbitListener(queues = {"queue1"})
    public void consumeMq(Message message, Channel channel) throws IOException {

        byte[] body = message.getBody();
        String msg = new String(body);
        // 线程池解析和处理报文
         poolDemoThread.thread();
         submit(() -> {

            // 解析
            int i = msg.indexOf("{");
            String substring = msg.substring(i);
            VehicleMsgEntity vehicleMsgEntity = JSON.parseObject(substring, VehicleMsgEntity.class);
            // 获取车辆vin
            String vin = vehicleMsgEntity.getVin();

            // redis 保存经纬度
            GeoOperations opsForGeo = redisTemplate.opsForGeo();
            Double longitude = Double.valueOf(vehicleMsgEntity.getLongitude());
            Double latitude = Double.valueOf(vehicleMsgEntity.getLatitude());
            opsForGeo.add("VehicleLGLA", new Point(longitude, latitude),"vehicle");

            // 车辆故障存入数据库
            HashOperations opsForHash = redisTemplate.opsForHash();
            // 获取车辆状态信息
            String vehicleStatusMsg = vehicleMsgEntity.getVehicleStatusMsg();

            // 判断是否是故障信息
                String[] split = vehicleStatusMsg.split(" ");
                for (int j = 0; j < split.length; j++) {
                    if (split[j].equals("00")) {
                        // 查询数据库是否存在故障码信息
                        String aa = "GTX00"+j;
                        String vinVehic = vin;
                        TbFailurelog failurelog =  faultLogMapper.getFaultOne(aa,vinVehic);
                        if (failurelog != null){
                            System.out.println("故障码数据存在");
                        }else{
                            // 不存在进行存储
                            TbFailurelog log = new TbFailurelog();
                            log.setStatusTime(new Date());
                            log.setVin(vin);
                            log.setFailureCode("GTX00" + j);
                            faultLogMapper.insertTbFailurelog(log);
                            // 故障码存入redis 不重复存入
                            opsForHash.put("breakdown:" + vin,"GTX00"+j,"GTX00"+j );
                        }
                    }else{
                        //  修改数据库的 结束报警时间
                        String aa = "GTX00"+j;
                        String vinVehic = vin;
                        TbFailurelog failurelog =  faultLogMapper.getFaultOne(aa,vinVehic);
                        if (failurelog == null){
                            continue;
                        }
                        TbFailurelog log2 = new TbFailurelog();
                        log2.setEndTime(new Date());
                        log2.setId(failurelog.getId());
                        faultLogMapper.updateTbFailurelog(log2);
                        // 删除 redis的故障信息
                        opsForHash.delete("breakdown:"+vin,"GTX00"+j);

                    }
                }

            // 获取智能系统异常
            String smartHardwareMsg = vehicleMsgEntity.getSmartHardwareMsg();
                String[] split1 = smartHardwareMsg.split(" ");
                for (int j = 0; j < split1.length; j++) {
                    if (split1[j].equals("00")) {
                        String aa = "GTO00"+j;
                        String vinVehic = vin;
                        TbFailurelog failurelog =  faultLogMapper.getFaultOne(aa,vinVehic);
                        if (failurelog != null){
                            System.out.println("故障码数据存在");
                        }else {

                            TbFailurelog log = new TbFailurelog();
                            log.setStatusTime(new Date());
                            log.setVin(vin);
                            log.setFailureCode("GTO00" + j);

                            faultLogMapper.insertTbFailurelog(log);
                            // 故障码存入redis 不重复存入
                            opsForHash.put("breakdown:" + vin,"GTO00"+j,"GTO00"+j );

                        }
                    }else{
                        //  修改数据库的 结束报警时间
                        String aa = "GTO00"+j;
                        String vinVehic = vin;
                        TbFailurelog failurelog =  faultLogMapper.getFaultOne(aa,vinVehic);
                        if (failurelog == null){
                            continue;
                        }
                        TbFailurelog log2 = new TbFailurelog();
                        log2.setEndTime(new Date());
                        log2.setId(failurelog.getId());
                        faultLogMapper.updateTbFailurelog(log2);
                        // 删除 redis的故障信息
                        opsForHash.delete("breakdown:"+vin,"GTO00"+j);
                    }
                }

            // 电池的异常
            String batteryMsg = vehicleMsgEntity.getBatteryMsg();
                String[] split2 = batteryMsg.split(" ");
                for (int j = 0; j < split2.length; j++) {
                    if (split2[j].equals("00")) {
                        String aa = "GTB00"+j;
                        String vinVehic = vin;
                        TbFailurelog failurelog =  faultLogMapper.getFaultOne(aa,vinVehic);
                        if (failurelog != null){
                            System.out.println("故障码数据存在");
                        }else {
                            TbFailurelog log = new TbFailurelog();
                            log.setStatusTime(new Date());
                            log.setVin(vin);
                            log.setFailureCode("GTB00" + j);

                            faultLogMapper.insertTbFailurelog(log);
                            // 故障码存入redis 不重复存入
                            opsForHash.put("breakdown:" + vin,"GTB00"+j,"GTB00"+j );
                        }
                    }else{
                        //  修改数据库的 结束报警时间
                        String aa = "GTB00"+j;
                        String vinVehic = vin;
                        TbFailurelog failurelog =  faultLogMapper.getFaultOne(aa,vinVehic);
                        if (failurelog == null){
                            continue;
                        }
                        TbFailurelog log2 = new TbFailurelog();
                        log2.setEndTime(new Date());
                        log2.setId(failurelog.getId());
                        faultLogMapper.updateTbFailurelog(log2);
                        // 删除 redis的故障信息
                        opsForHash.delete("breakdown:"+vin,"GTB00"+j);
                    }
                }

        });
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

    }


}
